# coding:utf-8
import requests
import json


class English:
    def __init__(self):
        # 获取所有组数据URL - 有2种情况，分为带names和分names情况
        self.a_group_url = 'https://api.huilianyingyu.com/Sys/GetDictionariesList'

        # 单个分组数据
        self.single_group_url = 'https://api.huilianyingyu.com/Video/GetVideoListNot'

    # 执行入口
    def run_data(self):
        group_data = self.all_group_data()
        self.single_group_data(group_data)

    # 获取所有组数据
    def all_group_data(self):
        request_param = {
            'grouping': '视频分组',
            'names': '靶向练习,简单英语,生活词汇,替换练习,新概念,循环练习,多情景语法,多情景语块,多情景句型,多情景单词,多图片单词,发音练习,ZM,SA,MC,LLE,ETP,EP,DE'
        }
        # 代理服务器的URL
        proxies = {'http': 'http://localhost:8888', 'https': 'http://localhost:8888'}

        r = requests.get(self.a_group_url, params=request_param, headers=self.create_headers(), proxies=proxies, verify=False)
        # r = requests.get(self.a_group_url, params=request_param, headers=self.create_headers())
        group_data = r.json().get('data')
        print(group_data)
        return group_data

    # 获取某个分组下的列表数据
    def single_group_data(self, group_data_list):
        for group_data in group_data_list:
            print('group_data:', group_data)
            group_id = group_data.get('id')
            request_json = {
                "gid": group_id,
                "page": {
                    "index": 1,
                    "rows": 100000
                }
            }
            proxies = {'http': 'http://localhost:8888', 'https': 'http://localhost:8888'}

            r = requests.post(self.single_group_url, json=request_json, headers=self.create_headers(), proxies=proxies, verify=False)
            # r = requests.post(self.group_url, json=request_json, headers=self.create_headers())
            print('json1:', r.json())
            request_param = {
                'grouping': '视频分组',
                'pid': group_data.get('id')
            }
            r = requests.get(self.a_group_url, params=request_param, headers=self.create_headers(), proxies=proxies, verify=False)
            print('json2:', r.json())

    # 生成请求 headers
    @staticmethod
    def create_headers():
        headers = {
            'Accept': 'application/json, text/plain, */*',
            'Accept-Language': 'zh-CN,zh;q=0.9',
            'Connection': 'keep-alive',
            'Host': 'api.huilianyingyu.com',
            'Origin': 'https://www.huilianyingyu.com',
            'Referer': 'https://www.huilianyingyu.com/',
            'Sec-Fetch-Dest': 'empty',
            'Sec-Fetch-Mode': 'cors',
            'Sec-Fetch-Site': 'same-site',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36',
            'id': '109027',
            'nonce': '31924139',
            'sec-ch-ua': '"Google Chrome";v="123", "Not:A-Brand";v="8", "Chromium";v="123"',
            'sec-ch-ua-mobile': '?0',
            'sec-ch-ua-platform': '"Windows"',
            'signature': 'af8a4a1d71a0c7d913ee065274c720cc',
            'timestamp': '1713637466000000'
        }
        return headers


if __name__ == '__main__':
    a = English()
    a.all_group_data()